﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;
using DLLPC;
using System.Threading;

namespace COER
{
    public partial class UC_RecipeSetting : UserControl
    {
        public UC_RecipeSetting()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                //if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                //{
                //    ((DLLPC.PCAP.PCButton)item).OperationRecord = Kernel.OPRec;
                //}

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        RecipeModel tempRecPrm;
        public void ShowForm()
        {
            ComboBoxFresh(cbxNames, true);

            tempRecPrm = ParameterHelper.CurParam;

            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ProcedureHelper_EQ.RecGridRefresh)
            {
                UpdateUI(tempRecPrm);
                ProcedureHelper_EQ.RecGridRefresh = false;
            }

            if (this.Visible == false) return;

            btnNew.Enabled = Kernel.OP.Level > 0;
            btnDel.Enabled = Kernel.OP.Level > 0;
            btnSave.Enabled = Kernel.OP.Level > 0;
        }

        /// <summary>
        /// 内容更新
        /// </summary>
        /// <param name="cbx"></param>
        /// <param name="changeSelIdx"></param>
        private void ComboBoxFresh(ComboBox cbx, bool changeSelIdx)
        {
            cbx.Items.Clear();
            cbx.Text = "";
            //string[] names = ParameterHelper.GetRecipeNames();
            List<string> tRecipes = ParameterHelper.mRecDAL.GetRecipes();

            if (tRecipes.Count > 0)
            {
                foreach (var item in tRecipes)
                {
                    cbx.Items.Add(item);
                }
            }

            cbx.SelectedIndex = tRecipes.IndexOf(ParameterHelper.SysParam.CurPRM, 0);
        }

        private void UpdateUI(RecipeModel rec)
        {
            RecGrid.SelectedObject = rec;
            RecGrid.Refresh();
        }

        CopyDailog frmNew;
        FormLaserBlockSel frmBlockSel;
        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');
            //if (VisionCore.Kernel.PW == 0)
            //{
            //    MessageBox.Show("请切换至工程师模式", "权限不够", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}
            switch (info[1])
            {
                case "Change":
                    {
                        if (!ProcedureHelper_EQ.EQ_Empty)
                        {
                            MessageBox.Show("请清空机台在席，再切换工单！");
                            return;
                        }

                        if (DialogResult.No == MessageBox.Show("是否调用新工单？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                        {
                            return;
                        }

                        ParameterHelper.SysParam.CurPRM = cbxNames.SelectedItem.ToString();
                        ParameterHelper.mSysDAL.Save(ParameterHelper.SysParam);

                        ParameterHelper.CurParam = ParameterHelper.mRecDAL.Load(ParameterHelper.SysParam.CurPRM);

                        //Kernel.CCDMark1.LoadCognexVPP(ParameterHelper.CurParam.PRM_Name);

                        cbxNames.SelectedIndex = cbxNames.Items.IndexOf(ParameterHelper.SysParam.CurPRM);
                        ProcedureHelper_Exc.LaserID_Manual = "";
                        ProcedureHelper_Exc.LaserID_AutoGet = "";
                        MessageBox.Show("工单调用成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        UpdateUI(ParameterHelper.CurParam);
                        tempRecPrm = ParameterHelper.CurParam;
                    }
                    break;
                case "New":
                    {
                        if (frmNew == null)
                        {
                            frmNew = new CopyDailog();
                        }
                        frmNew.ShowForm(ParameterHelper.mRecDAL.GetRecipes());

                        if (frmNew.Dr == DialogResult.No) return;

                        RecipeModel tempRec = new RecipeModel();
                        if (frmNew.IsCopy)
                        {
                            ParameterHelper.mRecDAL.Copy(frmNew.SrcName, frmNew.NewName);
                            tempRec = ParameterHelper.mRecDAL.Load(frmNew.NewName);
                            tempRec.PRM_Name = frmNew.NewName;
                            ParameterHelper.mRecDAL.Save(tempRec, frmNew.NewName);


                            cbxNames.Items.Add(frmNew.NewName);

                            MessageBox.Show("工单复制完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            ParameterHelper.mRecDAL.Copy(ParameterHelper.FileName_Default, frmNew.NewName);
                            tempRec.PRM_Name = frmNew.NewName;
                            ParameterHelper.mRecDAL.Save(tempRec, frmNew.NewName);
                            cbxNames.Items.Add(frmNew.NewName);


                            MessageBox.Show("工单新建完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        ComboBoxFresh(cbxNames, false);
                    }
                    break;
                case "Del":
                    {
                        if (cbxNames.SelectedItem.ToString() == ParameterHelper.SysParam.CurPRM)
                        {
                            MessageBox.Show("工单使用中，不能删除！");
                            return;
                        }

                        string strName = cbxNames.Text;
                        DialogResult dr = MessageBox.Show("确定删除此工单", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        if (dr == DialogResult.No)
                        {
                            return;
                        }

                        ParameterHelper.mRecDAL.Delete(cbxNames.SelectedItem.ToString());

                        cbxNames.Items.Remove(cbxNames.SelectedItem.ToString());
                        cbxNames.SelectedIndex = cbxNames.Items.IndexOf(ParameterHelper.SysParam.CurPRM);

                        MessageBox.Show("删除成功！");
                    }
                    break;
                case "Save":
                    {
                        if (cbxNames.SelectedItem.ToString().Trim() == ParameterHelper.SysParam.CurPRM && !ProcedureHelper_EQ.ManualMode)
                        {
                            MessageBox.Show("选择工单与当前使用工单相同！ 请先切手动");
                            return;
                        }

                        tempRecPrm = RecGrid.SelectedObject as RecipeModel;

                        ParameterHelper.mRecDAL.Save(tempRecPrm, tempRecPrm.PRM_Name);

                        MessageBox.Show("参数保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        if (ParameterHelper.CurParam.PRM_Name == tempRecPrm.PRM_Name)
                        {
                            ParameterHelper.CurParam = tempRecPrm;
                        }
                    }
                    break;
                case "SaveBlock":
                    {
                        Kernel.Exc.Laser1.GetBlockParams(ParameterHelper.CurParam.LaserParam.Blocks);
                        Thread.Sleep(2000);

                        for (int i = 0; i < ParameterHelper.CurParam.LaserParam.Blocks.Count; i++)
                        {
                            BlockInfo info1 = Kernel.Exc.Laser1.Blocks.Find(a => a.Idx == ParameterHelper.CurParam.LaserParam.Blocks[i].Idx);
                            ParameterHelper.CurParam.LaserParam.Blocks[i].X = info1.X;
                            ParameterHelper.CurParam.LaserParam.Blocks[i].Y = info1.Y;
                            ParameterHelper.CurParam.LaserParam.Blocks[i].Z =Math.Abs( info1.Z)> 5?0 : info1.Z;
                        }

                        ProcedureHelper_EQ.SaveRecParam();
                        ProcedureHelper_EQ.RecGridRefresh = true;
                    }
                    break;
                case "BlockSel":
                    {
                        if (frmBlockSel == null)
                        {
                            frmBlockSel = new FormLaserBlockSel();
                        }

                        frmBlockSel.ShowForm(ParameterHelper.CurParam.LaserParam.LaserBlockSel);
                    }
                    break;
            }
        }

        private void cbxNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            RecGrid.SelectedObject = ParameterHelper.mRecDAL.Load(cbxNames.SelectedItem.ToString());
        }
    }
}
